<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>
<head>
	<title>线路定义管理</title>
	<meta name="decorator" content="default"/>
<link href="${ctxStatic}/jquery-ui-1.12.1/jquery-ui.min.css" rel="stylesheet" />
<script src="${ctxStatic}/jquery-ui-1.12.1/jquery-ui.min.js" type="text/javascript"></script>

	<script type="text/javascript">
	
		$(document).ready(function() {
			//$("#name").focus();
/* 			 $("#siteIdList").select2({
				  tags: "true",
				  placeholder: "Select an option",
				  allowClear: true
				});
			$("#siteIdList").on("change", function() { $("#siteIdListVal").html($("#siteIdList").val());});
			$("#siteIdList").select2("container").find("ul.select2-choices").sortable({
			    containment: 'parent',
			    start: function() { $("#siteIdList").select2("onSortStart"); },
			    update: function() { $("#siteIdList").select2("onSortEnd"); }
			}); */
			typeChange($('#linetype').val());
			$("#inputForm").validate({
				rules: {
					linename: {remote: "${ctx}/line/tLine/checklinename?oldlinename=" + encodeURIComponent('${tLine.linename}')}
				},
				messages: {
					linename: {remote: "线路名称不能重复！"}
				},
				submitHandler: function(form){
					loading('正在提交，请稍等...');
					form.submit();
				},
				errorContainer: "#messageBox",
				errorPlacement: function(error, element) {
					$("#messageBox").text("输入有误，请先更正。");
					if (element.is(":checkbox")||element.is(":radio")||element.parent().is(".input-append")){
						error.appendTo(element.parent().parent());
					} else {
						error.insertAfter(element);
					}
				}
			});
			// 出发城市不能与到达城市相同
			$.validator.addMethod("sameCity", function(value, element) {
				var departcity = $('#departcity').val();
				var destcity = $('#destcity').val();
				if(departcity == destcity){
					return false;
				} 
				return true;
			}, "出发城市不能与到达城市相同");
			// 正整数
			$.validator.addMethod("positive", function(value, element) {
				if(value<=0){
					return false;
				}else {
					return true;
				}
			}, "不能为0");
			$.validator.addMethod("numberhalf", function(value, element) {
				if(value<=0){
					return false;
				}else {
					var str = value.toString().split('.');
					if (str[1] != null) {
						if (str[1].length != 1)
							return false;
						else { 
							if (parseInt(str[1]) != 5)
								return false;
							else
								return true;
						}	
					}else {
						return true;
					}
				}
			}, "只能为设定半小时的倍数");
		});
		function typeChange(value){
			if (value == null)
				value = $('#linetype').val();
					
			linekind = $('#linekind').val();
			departid = $('#departcity').val();
			destid = $('#destcity').val();
			
			var selected = "";
		
		 	//根据是否纯电动车展示修改纯电动车最大行驶路程
			if (value == 1){
				document.getElementById("estd").style.display = "block";
				document.getElementById("stops").style.display = "block";
				document.getElementById("picurl").style.display = "block";
				document.getElementById("trtype").style.display = "block";
				document.getElementById("estdays").setAttribute("class","span4 digits positive");
				document.getElementById("kindid").style.display = "none";
				$('#depart').html('出发地：');
				$('#dest').html('目的地：');
			}else if (value == 2 || value == 3){
				document.getElementById("estd").style.display = "none";
				document.getElementById("stops").style.display = "none";
				document.getElementById("picurl").style.display = "none";
				document.getElementById("trtype").style.display = "none";
				$(estdays).val(1);
				document.getElementById("kindid").style.display = "block";
				if (linekind == 1 && value == 2) {
					$('#depart').html('接机机场：');
					$('#dest').html('目的地：');
				} else if (linekind == 2 && value == 2) {
					$('#depart').html('出发地：');
					$('#dest').html('送机机场：');
				}else if (linekind == 1 && value == 3) {
					$('#depart').html('火车站：');
					$('#dest').html('目的地：');
				}else if (linekind == 2 && value == 3) {
					$('#depart').html('出发地：');
					$('#dest').html('火车站：');
				}
			}else {
				document.getElementById("estd").style.display = "none";
				document.getElementById("stops").style.display = "none";
				document.getElementById("picurl").style.display = "none";
				document.getElementById("trtype").style.display = "none";
				document.getElementById("kindid").style.display = "none";
				$(estdays).val(1);
				$('#depart').html('出发地：');
				$('#dest').html('目的地：');
			}
			var spottypes = '';

			if (value == '1' ) {
			spottypes = '0,b';
			url = "${ctx}/line/tLine/ajaxList";
			$.ajax({
		          dataType: 'json',
		            url:url,
		            data:{spottypes:spottypes},
		            type: 'POST',
		            cache:false,
		            success: function(data) {
		            	var obj = eval(data);
		            	jQuery(departcity).html('');
		            	$(departcity).val("");
		            	$(departcity).text("请选择");
		            	jQuery(departcity).append("<option value=''>请选择</option>");
		            	$.each(obj, function (i, item) {
		            		selected = "";
		            		if (departid != null && departid == item.siteid)
		            			selected = "selected=selected";
		            		jQuery(departcity).append("<option value='"+ item.siteid+"' "+selected+">"+ item.sitename+"</option>");
		            	});
		            },
		            error: function() {
		            		 alert("数据有误");
		            }
		        });
				spottypes = '0,2,b';
				$.ajax({
		          dataType: 'json',
		            url:url,
		            data:{spottypes:spottypes},
		            type: 'POST',
		            cache:false,
		            success: function(data) {
		            	var obj = eval(data);
		            	jQuery(destcity).html('');
		            	$(destcity).val("");
		            	jQuery(destcity).append("<option value=''>请选择</option>");
		            	$.each(obj, function (i, item) {
		            		selected = "";
		            		if (destid != null && destid == item.siteid)
		            			selected = "selected=selected";
		            		 jQuery(destcity).append("<option value='"+ item.siteid+"' "+selected+">"+ item.sitename+"</option>");
		            	});
		            },
		            error: function() {
		            		 alert("数据有误");
		            }
		        });
			}else if (linekind == 1){
				if (value == 2)
					spottypes = '9';
				else
					spottypes = '8';
				url = "${ctx}/line/tLine/ajaxList";
				$.ajax({
			          dataType: 'json',
			            url:url,
			            data:{spottypes:spottypes},
			            type: 'POST',
			            cache:false,
			            success: function(data) {
			            	var obj = eval(data);
			            	jQuery(departcity).html('');
			            	jQuery(departcity).append("<option value=''>请选择</option>");
			            	
			            	$.each(obj, function (i, item) {
			            		selected = "";
			            		if (departid != null && departid == item.siteid)
			            			selected = "selected=selected";
			            		jQuery(departcity).append("<option value='"+ item.siteid+"' "+selected+">"+ item.sitename+"</option>");
			            	});
			            },
			            error: function() {
			            		 alert("数据有误");
			            }
			        });
				spottypes = 'a';
				$.ajax({
			          dataType: 'json',
			            url:url,
			            data:{spottypes:spottypes},
			            type: 'POST',
			            cache:false,
			            success: function(data) {
			            	var obj = eval(data);
 			            	jQuery(destcity).html('');
			            	jQuery(destcity).append("<option value=''>请选择</option>");
			            	$.each(obj, function (i, item) {
			            		selected = "";
			            		if (destid != null && destid == item.siteid)
			            			selected = "selected=selected";
			            		 jQuery(destcity).append("<option value='"+ item.siteid+"' "+selected+">"+ item.sitename+"</option>");
			            	});
			            },
			            error: function() {
			            		 alert("数据有误");
			            }
			        });
			}else if (linekind == 2){
				if (value == 2)
					spottypes = '9';
				else
					spottypes = '8';
				url = "${ctx}/line/tLine/ajaxList";
				$.ajax({
			          dataType: 'json',
			            url:url,
			            data:{spottypes:spottypes},
			            type: 'POST',
			            cache:false,
			            success: function(data) {
			            	var obj = eval(data);
			            	jQuery(destcity).html('');
			            	jQuery(destcity).append("<option value=''>请选择</option>");
			            	$.each(obj, function (i, item) {
			            		selected = "";
			            		if (destid != null && destid == item.siteid)
			            			selected = "selected=selected";
			            		 jQuery(destcity).append("<option value='"+ item.siteid+"' "+selected+">"+ item.sitename+"</option>");
			            	});
			            },
			            error: function() {
			            		 alert("数据有误");
			            }
			        });
				spottypes = 'a';
				$.ajax({
			          dataType: 'json',
			            url:url,
			            data:{spottypes:spottypes},
			            type: 'POST',
			            cache:false,
			            success: function(data) {
			            	var obj = eval(data);
 			            	jQuery(departcity).html('');
			            	jQuery(departcity).append("<option value=''>请选择</option>");
			            	$.each(obj, function (i, item) {
			            		selected = "";
			            		if (departid != null && departid == item.siteid)
			            			selected = "selected=selected";
			            		jQuery(departcity).append("<option value='"+ item.siteid+"' "+selected+">"+ item.sitename+"</option>");
			            	});
			            },
			            error: function() {
			            		 alert("数据有误");
			            }
			        });
			}
		}
	</script>
</head>
<body>
	<ul class="nav nav-tabs">
		<li><a href="${ctx}/line/tLine/">线路定义列表</a></li>
		<li class="active"><a href="${ctx}/line/tLine/form?lineid=${tLine.lineid}">线路定义<shiro:hasPermission name="line:tLine:edit">${not empty tLine.lineid?'修改':'添加'}</shiro:hasPermission><shiro:lacksPermission name="line:tLine:edit">查看</shiro:lacksPermission></a></li>
	</ul><br/>
	<form:form id="inputForm" modelAttribute="tLine" action="${ctx}/line/tLine/save" method="post" class="form-horizontal">
		<form:hidden path="lineid"/>
		<sys:message content="${message}"/>		
		<div class="control-group">
			<label class="control-label">线路名称：</label>
			<div class="controls">
				<form:input path="linename" htmlEscape="false" maxlength="50" class="span4 required"/>
				<span class="help-inline"><font color="red">*</font> </span>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">线路类型：</label>
			<div class="controls">
				<form:select path="linetype" class="span4 required"
					onchange="typeChange(this.options[this.options.selectedIndex].value)">
					<form:option value="">请选择</form:option>
					<form:options items="${fns:getDictList('line_type')}"
						itemLabel="label" itemValue="value" htmlEscape="false" />
				</form:select>
				<span class="help-inline"><font color="red">*</font> </span>
			</div>
		</div>
		<div class="control-group" id="kindid">
			<label class="control-label">接送类型：</label>
			<div class="controls">
				<form:select path="linekind" class="span4" onchange="typeChange()">
					<form:options items="${fns:getDictList('linekind')}"
						itemLabel="label" itemValue="value" htmlEscape="false" />
				</form:select>
				<span class="help-inline"><font color="red">*</font> </span>
			</div>
		</div>
		<div class="control-group" id="picurl">
			<label class="control-label">线路图片:</label>
			<div class="controls">
				<form:hidden id="linepic" path="linepic" htmlEscape="false" maxlength="200" class="input-xlarge"/>
				<sys:ckfinder input="linepic" type="images" uploadPath="/linepic" selectMultiple="false" maxWidth="100" maxHeight="100"/>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label" id="depart">出发地：</label>
			<div class="controls">
				<select id="departcity" name="departcity.siteid"   class="span4 sameCity required">
					<option value=''>请选择</option>
					<c:forEach items="${siteList }" var="site">
						<option value="${site.siteid }"
						<c:if test="${ tLine.departcity.siteid eq site.siteid}">selected="selected"</c:if>>
						${site.sitename }</option>
					</c:forEach>
				</select>
				<span class="help-inline"><font color="red">*</font> </span>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label" id="dest">目的地：</label>
			<div class="controls">
				<select id="destcity" name="destcity.siteid" class="span4 required">
				<option value=''>请选择</option>
					<c:forEach items="${siteList }" var="site">
						<option value="${site.siteid }"
						<c:if test="${ tLine.destcity.siteid eq site.siteid}">selected="selected"</c:if>>
						${site.sitename }</option>
					</c:forEach>
				</select>
				<span class="help-inline"><font color="red">*</font> </span>
			</div>
		</div>
		<div class="control-group" id="stops">
			<label class="control-label">经停城市：</label>
			<div class="controls">
				<form:select path="siteIdList" class="span4">
	 				<form:options items="${siteList}" itemLabel="sitename" itemValue="siteid" htmlEscape="false"/> 
				</form:select>
			</div>
			<div id="siteIdListVal"></div>
		</div>
<%--		<div class="control-group">
			<label class="control-label">公里数：</label>
			<div class="controls">
				<form:input path="miles" htmlEscape="false" class="span4 required"/>
				<span class="help-inline"><font color="red">*</font> </span>
			</div>
		</div>
 		<div class="control-group">
			<label class="control-label">租赁起止日期：</label>
			<div class="controls">
				<input id="fromdate" name="fromdate" type="text" readonly="readonly" maxlength="20" class="input-mini Wdate required"
					value="<fmt:formatDate value="${tLine.fromdate}" pattern="yyyy-MM-dd"/>"
					onclick="WdatePicker({dateFmt:'yyyy-MM-dd',isShowClear:false,maxDate:'#F{$dp.$D(\'stopdate\')}'});"/>
				<span class="help-inline"><font color="red">*</font>－</span>
				<input id="stopdate" name="stopdate" type="text" readonly="readonly" maxlength="20" class="input-mini Wdate required"
					value="<fmt:formatDate value="${tLine.stopdate}" pattern="yyyy-MM-dd"/>"
					onclick="WdatePicker({dateFmt:'yyyy-MM-dd',isShowClear:false,minDate:'#F{$dp.$D(\'fromdate\')}'});"/>
				<span class="help-inline"><font color="red">*</font> </span>
			</div>
		</div> --%>
		<div class="control-group" id="estd">
			<label class="control-label">天数：</label>
			<div class="controls">
				<form:input path="estdays" htmlEscape="false" class="span4 digits positive"/>
				<span class="help-inline">天<font color="red">*</font> </span>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">线路描述：</label>
			<div class="controls">
				<form:textarea path="description" htmlEscape="false" rows="5" cols="3" maxlength="750" class="span4 "/>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">线路状态：</label>
			<div class="controls">
				<form:radiobutton path="linestatus" value="0"/>有效
				<form:radiobutton path="linestatus" value="1"/>无效
			</div>
		</div>
		<div class="control-group" id="trtype">
			<label class="control-label">旅游包车类型：</label>
			<div class="controls">
				<input type="radio" value="1" name="traveltype" ${tLine.traveltype==1?'checked':'' }>周边游
				<input type="radio" value="2" name="traveltype" ${tLine.traveltype==2?'checked':'' }>目的地游
			</div>
		</div>
		<div class="control-group" id="trtype">
			<label class="control-label">是否热门线路：</label>
			<div class="controls">
				<input type="radio" value="1" name="ishot" ${tLine.ishot==1?'checked':'' }>热门线路
				<input type="radio" value="0" name="ishot" ${tLine.ishot==0?'checked':'' }>非热门线路
			</div>
		</div>
		<div class="form-actions">
			<shiro:hasPermission name="line:tLine:edit"><input id="btnSubmit" class="btn btn-primary" type="submit" value="保 存"/>&nbsp;</shiro:hasPermission>
			<input id="btnCancel" class="btn" type="button" value="返 回" onclick="history.go(-1)"/>
		</div>
	</form:form>
	
</body>
</html>